package com.vcc.core.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import com.alibaba.fastjson2.JSON;
import com.aliyun.oss.ServiceException;
import com.vcc.core.exception.ProgramException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;

public class HttpUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    private static final String DEFAULT_CONTENT_TYPE = "application/json";

    /**
     * 创建请求体
     * @return requestBody
     */
    public static HashMap<String, Object> createBody() {
        return MapUtil.newHashMap();
    }

    /**
     * 创建请求参数
     * @return requestParams
     */
    public static HashMap<String, Object> createParam() {
        return MapUtil.newHashMap();
    }

    /**
     * 创建请求头
     * @return httpHeaders
     */
    public static HashMap<String, String> createHeader() {
        return MapUtil.newHashMap();
    }

    public static String get(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body, String contentType) {
        HttpResponse execute = execute(url, params, headers, body, Method.GET, contentType);
        return execute.body();
    }

    public static String get(String url, HashMap<String, Object> params, HashMap<String, String> headers) {
        HttpResponse execute = execute(url, params, headers, null, Method.GET, DEFAULT_CONTENT_TYPE);
        return execute.body();
    }

    public static String get(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body) {
        HttpResponse execute = execute(url, params, headers, body, Method.GET, DEFAULT_CONTENT_TYPE);
        return execute.body();
    }

    public static String post(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body, String contentType) {
        HttpResponse execute = execute(url, params, headers, body, Method.POST, contentType);
        return execute.body();
    }

    public static String post(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body) {
        return post(url, params, headers, body, DEFAULT_CONTENT_TYPE);
    }

    public static String post(String url, HashMap<String, String> headers, HashMap<String, Object> body) {
        return post(url, createParam(), headers, body, DEFAULT_CONTENT_TYPE);
    }

    public static String put(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body, String contentType) {
        HttpResponse execute = execute(url, params, headers, body, Method.PUT, contentType);
        return execute.body();
    }

    public static String put(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body) {
        return put(url, params, headers, body, DEFAULT_CONTENT_TYPE);
    }


    public static String delete(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body, String contentType) {
        HttpResponse execute = execute(url, params, headers, body, Method.DELETE, contentType);
        return execute.body();
    }

    public static String delete(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body) {
        return delete(url, params, headers, body, DEFAULT_CONTENT_TYPE);
    }

    public static String delete(String url, HashMap<String, String> headers) {
        return delete(url, null, headers, null, DEFAULT_CONTENT_TYPE);
    }

    public static HttpResponse execute(String url, HashMap<String, Object> params, HashMap<String, String> headers, HashMap<String, Object> body, Method method, String contentType) {
        logger.info("请求url:{}", url);
        logger.info("请求参数:{}", JSON.toJSONString(params));
        logger.info("请求头:{}", JSON.toJSONString(headers));
        logger.info("请求体:{}", JSON.toJSONString(body));
        logger.info("请求方法:{}", method);
        UrlBuilder urlBuilder = UrlBuilder.of(url);
        if (CollUtil.isNotEmpty(params)) {
            urlBuilder.setQuery(UrlQuery.of(params));
        }
        logger.info("请求拼接后url:{}", urlBuilder.build());
        HttpRequest httpRequest = HttpRequest.of(urlBuilder.build()).method(method);
        if (CollUtil.isNotEmpty(headers)) {
            headers.forEach(httpRequest::header);
        }
        if (CollUtil.isNotEmpty(body)) {
            httpRequest.body(JSON.toJSONString(body));
        }
        if (StringUtils.isNotBlank(contentType)) {
            httpRequest.contentType(contentType);
        }
        try (HttpResponse response = httpRequest.execute()) {
            logger.info("响应状态码:{}", response.getStatus());
            logger.info("响应头:{}", JSON.toJSONString(response.headers()));
            logger.info("响应体:{}", response.body());
            return response;
        } catch (Exception e) {
            logger.error("请求失败", e);
            throw new ProgramException("请求失败");
        }

    }

    public static InputStream download(String url) {
        byte[] bytes = cn.hutool.http.HttpUtil.downloadBytes(url);
        //把bytes转为inputStream
        return new ByteArrayInputStream(bytes);
    }


}
